<?php

class Zend_View_Helper_Categories extends Zend_View_Helper_BaseUrl
{
	private $_struct = array();
	private $_selected = array();
	
	public function Categories( $list = array(), $selected = array() ) {
		$this->_selected = $selected;
		
		foreach( $list->toArray() as $value ) {
			$this->_struct[$value['parent_id']][$value['c_id']] = $value;
		}
		if( isSet(  $this->_struct[0] ) ) {
			return $this->_getTree();
		} else {
			return;
		}
	}
	
	private function _getTree() {
		$html = '<ul>';
		foreach( $this->_struct[0] as $value ) {

			$sel = in_array( $value['c_id'], $this->_selected ) ? 'checked="checked"' : '';

			$html .= '<li><label><input ' . $sel . ' type="checkbox" name="categories[]" value="' . $value['c_id'] . '" /> ' . $value['name_pl'] . '</label>';
				if( isSet( $this->_struct[$value['c_id']] ) ) {
					$html .= '<ul>';
					foreach( $this->_struct[$value['c_id']] as $item ) {

						$sel2 = in_array( $item['c_id'], $this->_selected ) ? 'checked="checked"' : '';

						$html .= '<li class="pod"><label><input ' . $sel2 . ' type="checkbox" name="categories[]" value="' . $item['c_id'] . '" /> ' . $item['name_pl'] . '</label></li>';
					}
					$html .= '</ul>';
				}
			$html .= '</li>';
		}
		$html .= '</ul>';
		
		return $html;
	}
}